﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CarExam.Unitys
{
    public class EnitiyHelper
    {
        private static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString();

        /// <summary>
        /// 获取泛型类的所有属性值
        /// </summary>
        /// <typeparam name="T">实体类的类型</typeparam>
        /// <param name="model">实体类</param>
        /// <returns>实体类属性表</returns>
        public Hashtable GetGenericsClassPropertys<T>(T model)
        {
            Hashtable ht = new Hashtable();
            PropertyInfo[] properties = model.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return null;
            }
            foreach (PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(model, null);
                ht.Add(name, value);
            }
            return ht;
        }

        /// <summary>
        /// 向数据库里添加数据
        /// </summary>
        /// <param name="GCP">实体类属性组</param>
        /// <param name="DATA_TableName">表名</param>
        /// <returns>影响的行数</returns>
        public int ExecGenericsInsert(Hashtable GCP, string DATA_TableName)
        {
            int i = 0;
            StringBuilder sqlstring = new StringBuilder();

            sqlstring.Append("INSERT INTO " + DATA_TableName + " (");

            foreach (DictionaryEntry item in GCP)
            {
                sqlstring.Append(item.Key.ToString() + ",");
            }
            sqlstring.Remove(sqlstring.Length - 1, 1);
            sqlstring.Append(")");

            sqlstring.Append(" VALUES(");

            foreach (DictionaryEntry item in GCP)
            {
                sqlstring.Append("@" + item.Key.ToString() + ",");
            }
            sqlstring.Remove(sqlstring.Length - 1, 1);
            sqlstring.Append(")");

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sqlstring.ToString(), connection))
                {
                    try
                    {
                        foreach (DictionaryEntry item in GCP)
                        {
                            cmd.Parameters.AddWithValue(item.Key.ToString(), item.Value);
                        }
                        connection.Open();
                        i = cmd.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            return i;
        }

        /// <summary>
        /// 更新数据库数据，需要移除hashtable中不需要更新的字段，如id字段
        /// </summary>
        /// <param name="GCP">实体类属性集合</param>
        /// <param name="DATA_TableName">数据库表名</param>
        /// <param name="tianjiao">sql语句中的where条件</param>
        /// <returns></returns>
        public int ExecGenericsUpdate(Hashtable GCP, string DATA_TableName, string tiaojian)
        {
            int i = 0;
            StringBuilder sqlstring = new StringBuilder();

            sqlstring.Append("UPDATE " + DATA_TableName + " SET ");
            foreach (DictionaryEntry item in GCP)
            {
                sqlstring.AppendFormat("{0}=CONVERT(nvarchar,{1}),", item.Key, "@" + item.Key);
            }
            sqlstring.Remove(sqlstring.Length - 1, 1);
            sqlstring.Append(tiaojian);

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sqlstring.ToString(), connection))
                {
                    try
                    {
                        foreach (DictionaryEntry item in GCP)
                        {
                            cmd.Parameters.AddWithValue(item.Key.ToString(), item.Value);
                        }
                        connection.Open();
                        i = cmd.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }

            return i;
        }
    }
}

